//+------------------------------------------------------------------+
//| 五浪理论遗传算法指标
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrBlue
#property indicator_width1  2

// 输入参数
input int    WaveLookback = 100;          // 波浪回看周期
input double MinWaveAmplitude = 0.0001;   // 最小波浪幅度
input double MaxWaveRetracement = 0.786;  // 最大回撤比例
input double MinWaveExtension = 1.0;      // 最小延伸比例

// 指标缓冲区
double SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function
//+------------------------------------------------------------------+
int OnInit() {
    // 设置指标缓冲区
    SetIndexBuffer(0, SignalBuffer, INDICATOR_DATA);
    PlotIndexSetInteger(0, PLOT_ARROW, 233);
    
    // 设置指标名称
    IndicatorSetString(INDICATOR_SHORTNAME, "Wave Theory GA");
    
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
    
    // 检查数据是否足够
    if(rates_total < WaveLookback) return(0);
    
    // 计算起始位置
    int start = prev_calculated == 0 ? WaveLookback : prev_calculated - 1;
    
    // 主循环
    for(int i = start; i < rates_total; i++) {
        // 初始化信号
        SignalBuffer[i] = 0;
        
        // 检查是否形成完整的五浪结构
        if(IsCompleteWaveStructure(i, high, low, close)) {
            // 生成交易信号
            SignalBuffer[i] = GenerateSignal(i, high, low, close);
        }
    }
    
    return(rates_total);
}

//+------------------------------------------------------------------+
//| 检查是否形成完整的五浪结构
//+------------------------------------------------------------------+
bool IsCompleteWaveStructure(int index, const double &high[], const double &low[], const double &close[]) {
    // 检查第1浪
    if(!IsWave1(index, high, low)) return false;
    
    // 检查第2浪
    if(!IsWave2(index, high, low)) return false;
    
    // 检查第3浪
    if(!IsWave3(index, high, low)) return false;
    
    // 检查第4浪
    if(!IsWave4(index, high, low)) return false;
    
    // 检查第5浪
    if(!IsWave5(index, high, low)) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查第1浪
//+------------------------------------------------------------------+
bool IsWave1(int index, const double &high[], const double &low[]) {
    // 检查幅度
    double amplitude = high[index] - low[index];
    if(amplitude < MinWaveAmplitude) return false;
    
    // 检查趋势
    if(close[index] <= close[index-1]) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查第2浪
//+------------------------------------------------------------------+
bool IsWave2(int index, const double &high[], const double &low[]) {
    // 检查回撤
    double wave1High = high[index-1];
    double wave1Low = low[index-1];
    double wave2Low = low[index];
    
    double retracement = (wave1High - wave2Low) / (wave1High - wave1Low);
    if(retracement > MaxWaveRetracement) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查第3浪
//+------------------------------------------------------------------+
bool IsWave3(int index, const double &high[], const double &low[]) {
    // 检查延伸
    double wave2Low = low[index-1];
    double wave3High = high[index];
    double wave1High = high[index-2];
    
    double extension = (wave3High - wave2Low) / (wave1High - wave2Low);
    if(extension < MinWaveExtension) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查第4浪
//+------------------------------------------------------------------+
bool IsWave4(int index, const double &high[], const double &low[]) {
    // 检查回撤
    double wave3High = high[index-1];
    double wave3Low = low[index-1];
    double wave4Low = low[index];
    
    double retracement = (wave3High - wave4Low) / (wave3High - wave3Low);
    if(retracement > MaxWaveRetracement) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 检查第5浪
//+------------------------------------------------------------------+
bool IsWave5(int index, const double &high[], const double &low[]) {
    // 检查是否创新高
    if(high[index] <= high[index-1]) return false;
    
    return true;
}

//+------------------------------------------------------------------+
//| 生成交易信号
//+------------------------------------------------------------------+
double GenerateSignal(int index, const double &high[], const double &low[], const double &close[]) {
    // 计算波浪强度
    double waveStrength = CalculateWaveStrength(index, high, low, close);
    
    // 根据波浪强度生成信号
    if(waveStrength > 0.7) return 1.0;    // 强烈买入信号
    if(waveStrength < -0.7) return -1.0;  // 强烈卖出信号
    
    return 0.0;  // 无信号
}

//+------------------------------------------------------------------+
//| 计算波浪强度
//+------------------------------------------------------------------+
double CalculateWaveStrength(int index, const double &high[], const double &low[], const double &close[]) {
    // 计算第3浪强度
    double wave3Strength = (high[index-2] - low[index-2]) / (high[index-3] - low[index-3]);
    
    // 计算第5浪强度
    double wave5Strength = (high[index] - low[index]) / (high[index-1] - low[index-1]);
    
    // 计算整体强度
    double totalStrength = (wave3Strength + wave5Strength) / 2.0;
    
    // 根据趋势方向调整强度
    if(close[index] < close[index-1]) {
        totalStrength = -totalStrength;
    }
    
    return totalStrength;
} 